//
//  AddNewFriendCenter.m
//  HiQ
//
//  Created by shoguncao on 12-8-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AddNewFriendCenter.h"
#import "MacrosDefine.h"
#import "MainWindowController.h"
#import "AddNewFriendWindowController.h"
#import "AddNewFriendRequestWindowController.h"
#import "SCNotificationUtils.h"
#import "Connection.h"

@implementation AddNewFriendCenter

- (void)onSendAddNewFriendRequest:(NSInteger)fromId to:(NSInteger)toId
{
    NSMutableData *packet = [[NSMutableData alloc] initWithBytes:&fromId length:sizeof(fromId)];
    [packet appendBytes:&toId length:sizeof(toId)];
    
    [[Connection sharedServerConnection] sendData:packet cmd:CMD_FUN_ADD_NEW_FRIEND subcmd:OP_ADD_REQUEST];
}

- (void)onReceiveAddNewFriendReply:(NSData *)data
{
    UTF8Char * bytes = (UTF8Char *)[data bytes];
    NSUInteger operation = *((NSUInteger *)(bytes+8));
    
    switch (operation) {
        case OP_SEARCH_NEW_FRIEND:
        {
            [self getSearchedUserInfo:data];
            break;
        }
        
        case OP_ADD_REQUEST:
        {
            NSInteger fromId = *((NSUInteger *)(bytes+16));
            NSInteger toId = *((NSUInteger *)(bytes+24));
            NSString *title = [[NSString alloc] initWithFormat:NSLocalizedString(@"%ld 请求添加您为好友", nil), fromId];
            [[MainWindowController sharedMainWindowController] prepareAddNewFriendWindowForGrowl:fromId to:toId];
            [SCNotificationUtils notifyAddNewFriendWithTitle:title description:nil];
            break;
        }
            
        case OP_ADD_REPLY:
        {
            NSInteger fromId = *((NSUInteger *)(bytes+16));
            NSInteger toId = *((NSUInteger *)(bytes+24));
            NSInteger isAccept = *((NSUInteger *)(bytes+32));
            [[MainWindowController sharedMainWindowController] prepareIsAcceptAddNewFriendWindowForGrowl:isAccept fromId:fromId to:toId];
            NSString *title;
            if (isAccept == REPLY_ACCEPT)
            {
                title = [[NSString alloc] initWithFormat:NSLocalizedString(@"%ld 同意了您添加好友的请求", nil), toId];
                [SCNotificationUtils notifyIsAcceptAddNewFriendWithTitle:title description:nil];
            }
            else if (isAccept == REPLY_REJECT)
            {
                title = [[NSString alloc] initWithFormat:NSLocalizedString(@"%ld 拒绝了您添加好友的请求", nil), toId];
                [SCNotificationUtils notifyIsAcceptAddNewFriendWithTitle:title description:nil];
            }
            else if (isAccept == REPLY_OFFLINE)
            {
                title = [[NSString alloc] initWithFormat:NSLocalizedString(@"%ld 不在线", nil), toId];
                [SCNotificationUtils notifyIsAcceptAddNewFriendWithTitle:title description:nil];
            }
            break;
        }
            
        default:
            break;
    }
}

- (void)searchForUser:(NSInteger)qqNum
{
    NSMutableData *packet = [[NSMutableData alloc] initWithBytes:&qqNum length:sizeof(qqNum)];
    
    [[Connection sharedServerConnection] sendData:packet cmd:CMD_FUN_ADD_NEW_FRIEND subcmd:OP_SEARCH_NEW_FRIEND];
}

- (void)getSearchedUserInfo:(NSData *)data
{
    char *packet = (char *)[data bytes];
    char *ptr = packet;
    
    ptr = ptr + 2*sizeof(NSInteger);
    NSInteger userInfoLen = *((NSInteger *)ptr);   // userInfo 信息长度
    ptr = ptr + sizeof(NSInteger);
    if (userInfoLen == 0)
    {
        AddNewFriendWindowController *addNewFriendWindowController = [[MainWindowController sharedMainWindowController] addNewFriendWindowController];
        addNewFriendWindowController->isfindBuddy = NO;
    }
    else
    {
        NSData *userInfoData = [[NSData alloc] initWithBytes:ptr length:userInfoLen];
        NSMutableArray *userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];
        AddNewFriendWindowController *addNewFriendWindowController = [[MainWindowController sharedMainWindowController] addNewFriendWindowController];
        addNewFriendWindowController->isfindBuddy = YES;
        addNewFriendWindowController->buddyInfo->buddyQQ = [[userInfo objectAtIndex:0] intValue];
        addNewFriendWindowController->buddyInfo->buddyName = [userInfo objectAtIndex:1];
        addNewFriendWindowController->buddyInfo->photo = [userInfo objectAtIndex:3];
        addNewFriendWindowController->buddyInfo->onPhoto = [NSImage imageNamed:[userInfo objectAtIndex:3]];
    }
    //当需要更新ui界面时，转回主线程，否则ui刷新会延迟
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:GetNewFriendInformation object:nil userInfo:nil];
    });
}

- (void)sendAddFriendReply:(NSInteger)isAccept fromId:(NSInteger)fromId toId:(NSInteger)toId
{
    NSMutableData *packet = [[NSMutableData alloc] initWithBytes:&fromId length:sizeof(fromId)];
    [packet appendBytes:&toId length:sizeof(toId)];
    [packet appendBytes:&isAccept length:sizeof(isAccept)];
    
    [[Connection sharedServerConnection] sendData:packet cmd:CMD_FUN_ADD_NEW_FRIEND subcmd:OP_ADD_REPLY];
}

@end
